<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * M_login
 *
 * This is an M_login of a few basic login interaction methods you could use
 * all done with a hardcoded array.
 *
 * @package		CodeIgniter
 * @subpackage	Rest Server
 * @category	Controller
 * @author		Phil Sturgeon
 
*/

// This can be removed if you use __autoload() in config.php OR use Modular Extensions
require APPPATH.'/libraries/REST_Controller.php';

class Signup extends REST_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->load->model('mdl_signup');
		$this->load->model('mdl_apps');
	}
/*	
	function login_get()
    {
        
		
		$rows = array();
		$login = array();
		
		
		$page=$this->get('page');
		$limit=$this->get('lim');
		$key=$this->get('key');
		$value=$this->get('val');
		$countitem = 0;
		if (($key!=null) && ($page!=null) ){
			
			$arrwhere=$key." LIKE '%".$value."%'";
			$query = $this->mdl_login->get_item($limit, $page, $arrwhere);
			$countitem = $this->mdl_login->get_countitem($arrwhere);
		}elseif($limit!=null){
		
			$query = $this->mdl_login->get_item($limit, $page);
			$countitem = $this->mdl_login->get_countitem();
		}else{
			$query = $this->mdl_login->get_item();
			$countitem = $this->mdl_login->get_countitem();
		}
		
		if ($query->num_rows() > 0)
		{
			
			$i=0;
			foreach($query->result_array() as $row)
			{
				$rows[$i]['userid'] = $row['userid'];
				$rows[$i]['username'] = $row['username'];
				$rows[$i]['password'] = $row['password'];
				$rows[$i]['level_id'] = $row['level_id'];

				$i++;
			}
			$data = array(
				'countitem' => $countitem,
				'datas' => $rows
				
			);
		}
		
		//print_r($data);
		
        if($data)
        {
            $this->response($data, 200); // 200 being the HTTP response code
        }

        else
        {
            $this->response(array('error' => 'Items could not be found'), 404);
        } 
    }
    */
   /* function login_post()
    {
        //$this->some_model->updatelogin( $this->get('id') );
     
		
$data['username']=$this->post('username'); 
$data['password']=$this->post('password'); 
$data['level_id']=$this->post('level_id'); 



		$query = $this->mdl_login->insert($data);
		
	
		if ($query){
			
			$this->response(array('status' => 'true'),200);
		}else{
			$this->response(array('status' => 'false'));
		}

    }
	*/
	
	function check_signup_post(){
		   
		//post from apps
		
		$username=$this->post('username'); 
		$password=$this->post('password'); 
		$appsidonapps=$this->post('appsidonapps'); 
		$ip=$this->post('ip'); 
		$device=$this->post('device');
		$os=$this->post('os'); 
		$location=$this->post('location'); 
		$phone=$this->post('phone'); 
		$country=$this->post('country'); 
		$city=$this->post('city'); 
		
		// variables
		
		$response = array();
		$signupcheck = 0;
		$appsidonappscheck = 0;
		$loginstatus=0;
		$firstimestatus = 0;
		$verificationstatus=0;
		$pinstatus = 'false';
		$pinnotification = 'false';
		$appsidonserver = '';
		$appsver = '';
		$appsstatus = 'true';
		$verificationstatus = 'false';
		$verificationnotification = 'false';
		
		// --- start check username
		
		$arrwhere="username = '" . $username . "'";
		$query = $this->mdl_signup->get_item(0, 0, $arrwhere);
		
		
		if ($query->num_rows() > 0)
		{
			$i=0;
			foreach($query->result_array() as $row)
			{
				$rows[$i]['username'] = $row['username'];
				$i++;
			}
		
			$signupcheck = 1; // username exist
				
		}
		// --- end check username
		
				
// ---start email address udah ada

						
			if ($signupcheck){  
				$status = array( array(
				"arrtitle" => "allstatus",
				"signupstatus" => "false",
				"appsidonapps" => "false",
				"signupnotification" => "Email anda sudah terdaftar. Gunakan email yang lain.",
				'debug'=> ' gagal signup email udah ada'
				));
			
			    
$this->response($status, 200);		
		
// ---end email address udah ada			
			} else			 	
			{
// -----start  email belum ada 			
			$data = array();
			$data['username'] = $username;
			$data['password'] = md5($password);
			$data['phone'] = $phone;
			$data['country'] = $country;
			$data['city'] = $city;
			$data['appsid'] = $appsidonapps;
			
			$this->load->helper('jwt_helper');
			$token = array();
			$token['username'] = $username;
			$token['appsidonapps'] = $appsidonapps;
			$verificationtoken=JWT::encode($token, 'dompeto');
			$data['verificationtoken'] = $verificationtoken; // BIKIN TOKEN VERIFICATION UNTUK DIKIRIM KE EMAIL BERDASAR USERNAME DAN APPS ID
			
// ------start default wallet
			$wallet = array();
			$wallet[] = array( 'walletprofile' => 
				array(
				"pagesize" => "1",
				"channelperpagesize" => "5",
				"channelsize" => "1"));
			$wallet[] = 
				array('pages' =>
					array(array( 'pageid'=> '1',
							"title" => "Discount Card",
							'slots' => 
							array(array('slotid'=>'1',
							'channelid'=>'1'
							),
							array('slotid'=>'2',
							'channelid'=>'5'
							),
							array('slotid'=>'3',
							'channelid'=>'false'
							),
							array('slotid'=>'4',
							'channelid'=>'false'
							),
							array('slotid'=>'5',
							'channelid'=>'false'
							)
							)
							
							),
							array( 'pageid'=> '2',
							"title" => "Shopping Card",
							'slots' => 
							array(array('slotid'=>'1',
							'channelid'=>'false'
							),
							array('slotid'=>'2',
							'channelid'=>'false'
							),
							array('slotid'=>'3',
							'channelid'=>'false'
							),
							array('slotid'=>'4',
							'channelid'=>'false'
							),
							array('slotid'=>'5',
							'channelid'=>'false'
							)
							)						
							
							
							)
							)
					);
						
		$data['wallet'] = serialize($wallet);					
//------- end default wallet		
				
						
//-------start default lang
			$langdefault = array(
				'langid' => 'id',
				'variabel1' => 'arti_1',
			'variabel1' => 'arti_1',
			'variabel2' => 'arti_2',
			'variabel3' => 'arti_3',
			'variabel4' => 'arti_4',
			'variabel5' => 'arti_5',
			'variabel6' => 'arti_6'
											
				);
				
				$data['lang'] = serialize($langdefault);			
				$data['langid'] = $langdefault['langid'];		
// ---------end default lang
							
			
			
			
			
			$query = $this->mdl_signup->insert($data);   // INSERT KE TABEL TUSERS
						
				
				
// --- cek appsid nya ada ngak ya				
			
			if($appsidonapps) {
			$arrwhere="appsid = '" . $appsidonapps . "'";
			$query = $this->mdl_apps->get_item(0, 0, $arrwhere);
				if ($query->num_rows() > 0)
						{
							$i=0;
							foreach($query->result_array() as $row)
							{
								$rows[$i]['appsid'] = $row['appsid'];
								$i++;
							}
						$query = $this->mdl_signup->updateappsid($username, $appsidonapps);
							$wallet = array(array(
								"arrtitle" => "allstatus",
								"signupstatus" => "true",
								"appsidonapps" => "true",
								"signupnotification" => "Your signup process succeed",
								'debug'=> ' sukses signup  ada appsid di tapps'
								));
								$this->response($wallet, 200);	
						} else {
	
$wallet = array(array(
				"arrtitle" => "allstatus",
				"signupstatus" => "true",
				"appsidonapps" => "false",
				"signupnotification" => "Your signup process succeed",
				'debug'=> ' sukses signup  ada appsid ngak ada di tapps'
				));
		 $this->response($wallet, 200);	
		
		}
			} else {
		
			
			$wallet = array(array(
				"arrtitle" => "allstatus",
				"signupstatus" => "true",
				"appsidonapps" => "false",
				"signupnotification" => "Your signup process succeed",
				'debug'=> ' signup success tapi apps id ngak ada didevice'
				));
			
			 $this->response(($wallet), 200);		
			
			}
		
			}
		
		
	}
	
	
    /*
    function login_delete()
    {
		
    	//$this->some_model->deletesomething( $this->get('id') );$this->_get('id')
        $message = array('message' => 'DELETED!', 'id'=>$this->delete('id'));
		
		$query = $this->mdl_login->delete($this->delete('id'));
		//$this->response($message,200);
		//$query=1;
		if ($query){
			$this->response(array('status' => 1),200);
		
		}else{
			$this->response(array('status' => 0));
		}
      
    } */
	/*
	 function login_put()
    {
		
    	//$this->some_model->deletesomething( $this->get('id') );$this->_get('id')
        //$message = array('message' => 'UPDATE!', 'userid'=>$this->delete('userid'), 'name'=>$this->delete('name'));
		$data['userid']=$this->put('userid'); 
$data['username']=$this->put('username'); 
$data['password']=$this->put('password'); 
$data['level_id']=$this->put('level_id'); 

		$query = $this->mdl_login->update($data['userid'],$data);
		//$this->response($message,200);
		
		if ($query){
			$this->response(array('status' => 1),200);
		
		}else{
			$this->response(array('status' => 0));
		}
      
    } */
    /*
    function logins_get()
    {
        //$logins = $this->some_model->getSomething( $this->get('limit') );
        $logins = array(
			array('id' => 1, 'name' => 'Some Guy', 'email' => 'example1@example.com'),
			array('id' => 2, 'name' => 'Person Face', 'email' => 'example2@example.com'),
			3 => array('id' => 3, 'name' => 'Scotty', 'email' => 'example3@example.com', 'fact' => array('hobbies' => array('fartings', 'bikes'))),
		);
        
        if($logins)
        {
            $this->response($logins, 200); // 200 being the HTTP response code
        }

        else
        {
            $this->response(array('error' => 'Couldn\'t find any items!'), 404);
        }
    } */
/*

	public function send_post()
	{
		var_dump($this->request->body);
	}
*/
/*
	public function send_put()
	{
		var_dump($this->put('foo'));
	}
	*/
	
	
	function countries_get()
	{
		 
		$arrwhere='active = 1 ';
		$query = $this->mdl_signup->get_countries(0, 0, $arrwhere);
		if ($query->num_rows() > 0)
		{
			$i=0;
			foreach($query->result_array() as $row)
			{ 
				$i++;
				$output[]=$row;  
				
			} 
		}
		
		$this->response($output, 200);	
	}
	
	function cities_post()
	{
		$idcountries=$this->post('idcountries');
		//$idcountries=101; 
		
		$arrwhere="active = '1' AND idcountries = '".$idcountries."'";
		
		$query = $this->mdl_signup->get_cities(0, 0, $arrwhere);
		if ($query->num_rows() > 0)
		{
			$i=0;
			foreach($query->result_array() as $row)
			{ 
				$i++;
				$output[]=$row;  
			} 
			$this->response($output, 200);	
		}
		else
		{ 
			$output = array('err' => 'false');
			$this->response($output, 200);	
		}
		
	}
}